package edu.pku.sei.transformation.editor.controller.editparts.node;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editpolicies.DirectEditPolicy;

import edu.pku.sei.modeler.edit.parts.AbstractNodeEditPart;
import edu.pku.sei.modeler.model.core.MetaModel;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.controller.policies.NodeComponentEditPolicy;
import edu.pku.sei.transformation.editor.controller.policies.TransformationNodeEditPolicy;
import edu.pku.sei.transformation.editor.views.figures.TagedElementFigure;
import edu.pku.sei.transformation.editor.views.icons.IconsProvider;

public class MetaModelEditpart extends AbstractNodeEditPart {

	public MetaModelEditpart(MetaShapeElement model) {
		super(model);
	}

	@Override
	protected DirectEditPolicy createDirectEditPolicy() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected IFigure createFigure() {
		MetaModel metamodel = (MetaModel) getNode().getModelElement();
		TagedElementFigure figure = new TagedElementFigure();
		figure.setTag("<<Meta-Model>>");
		figure.setName(metamodel.getName());
		figure.setNameLabelIcon(IconsProvider.getImage(IconsProvider.MODEL_ICO));
		return figure;
	}
	
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE,
				new TransformationNodeEditPolicy());
		installEditPolicy(EditPolicy.COMPONENT_ROLE,
				new NodeComponentEditPolicy());
	}
	
	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		TagedElementFigure figure = (TagedElementFigure) getFigure();
		if (event.getPropertyName().equals("name"))
			figure.setName(event.getNewValue().toString());
		else 
			super.handlePropertyChanged(event);
	}
}
